Parsing Custom Events with C#

Instead of using the Monitor utility to convert log folders into human-readable text files, you can do so programatically. For example, to parse custom events generated with RtGenerateEvent, you can use the following C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntervalZero.RTX64.Monitor;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            EventReader eventReader;

            try
            {
                // Attempts to get the latest log folder. If none is specified, chooses latest based on last in the alphanumeric naming and the log folder path set through the Control Panel.
                eventReader = new EventReader();
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error creating event reader");
                        
                return;
            }

            TextWriter writer;
            try
            {
                // Write this data to a file.
                writer = File.CreateText("C:\\log\\Convertedtext.txt");
            }
            catch (Exception exception)
            {
                Console.WriteLine("Error converting log file");
                return;
            }

            var fileSize = 0;
            var fileNumber = 0;
            var baseFileInfo = new FileInfo("C:\\log\\Convertedtext.txt");

            do
            {
                try
                {
                    // Get 100 event objects at a time.
                    var readEvents = eventReader.ReadEvents(100);
                    if (readEvents == null) break;

                    foreach (MonitorEvent logEvent in readEvents)
                    {
                        // If it's a custom event then write it to a file.
                       
                        if (logEvent.kind == (uint)MF_EVENT_KIND.MF_EVENT_KIND_CUSTOM)
                        {
                            writer.WriteLine(logEvent.ToString());
                            MonitorEventCustom customEvent = logEvent as MonitorEventCustom; 
                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine("Error parsing events.\n{0}");
                    break;
                }

            } while (true); // End of do loop

        } //End of main
    }
}

NOTE: EventReader will use the path set in the RTX64 Control Panel or will use a directory as an argument.

NOTE: In Tracealyzer, custom events are displayed as user events.

Related Topics: